package logbrowse;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

@SuppressWarnings("serial")
public class BrowseFrame extends JFrame
		implements ActionListener, ListSelectionListener {
	private JFileChooser chooser;
	private JButton open;
	private ViewLogSelector list;
	private ViewCodeHeader header0;
	private ViewCodeHeader header1;
	private ViewCode code0;
	private ViewCode code1;
	
	public BrowseFrame(LogModel model) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("pbLua Log Browser");
		
		chooser = new JFileChooser();
		if (model.getSourceFile() != null) {
			chooser.setCurrentDirectory(model.getSourceFile().getParentFile());
		} else {
			String cwd = System.getProperty("user.dir");
			if (cwd != null) {
				chooser.setCurrentDirectory(new File(cwd));
			}
		}
		
		open = new JButton("Open...");
		open.addActionListener(this);
		list = new ViewLogSelector(model);
		list.addListSelectionListener(this);
		code0 = new ViewCode();
		code1 = new ViewCode();
		header0 = new ViewCodeHeader();
		header1 = new ViewCodeHeader();
		JPanel header = new JPanel(new GridLayout(1, 2));
		header.add(header0);
		header.add(header1);
		JPanel main = new JPanel(new GridLayout(1, 2));
		main.add(code0);
		main.add(code1);
		JScrollPane mainScroll = new JScrollPane(main);
		mainScroll.setColumnHeaderView(header);
		
		JPanel buttons = new JPanel();
		buttons.add(open);
		
		Container contents = getContentPane();
		contents.add(buttons, BorderLayout.NORTH);
		contents.add(new JScrollPane(list), BorderLayout.WEST);
		contents.add(mainScroll, BorderLayout.CENTER);
		
		this.pack();
		this.setSize(600, 400);
	}
	
	public void actionPerformed(ActionEvent e) {
		Object src = e.getSource();
		if (src == open) {
			doOpen();
		}
	}
	
	private void doOpen() {
		int action = chooser.showOpenDialog(this);
		if (action == JFileChooser.APPROVE_OPTION) {
			try {
				LogModel model = LogModel.openFile(chooser.getSelectedFile());
				list.setLogModel(model);
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, "File could not be loaded.",
						"Error Loading File", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
			}
		}
	}

	public void valueChanged(ListSelectionEvent e) {
		int[] selected = list.getSelectedIndices();
		int sel0;
		int sel1;
		if (selected == null || selected.length == 0) {
			sel0 = -1;
			sel1 = -1;
		} else if (selected.length == 1) {
			sel0 = selected[0] - 1;
			sel1 = selected[0];
		} else {
			sel0 = selected[0];
			sel1 = selected[selected.length - 1];
		}
		LogModel model = list.getLogModel();
		LogEntry e0 = sel0 >= 0 ? model.getEntry(sel0) : null;
		LogEntry e1 = sel1 >= 0 ? model.getEntry(sel1) : null;
		String code0 = e0 == null ? "" : e0.getText();
		String code1 = e1 == null ? "" : e1.getText();
		LogEntryDiff diff = LogEntryDiff.compute(code0, code1);
		header0.setContents(e0);
		header1.setContents(e1);
		this.code0.setContents(e0, diff, 0);
		this.code1.setContents(e1, diff, 1);
	}
}
